package com.ys.utils.base.secret;


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5 加密类
 * @author sj
 *
 */
public class MD5 {
	
	public static String md5(String plainText) {
		String str = null;
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(plainText.getBytes());
			byte b[] = md.digest();

			int i;

			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
			str = buf.toString();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return str;
	}
	public static void main(String[] args) throws NoSuchAlgorithmException {
		System.out.println(md5("e5f838b91011f11862268b328e6891df#6633a96fd10738f413768a94391f4151"));
		
		MessageDigest digest=MessageDigest.getInstance("MD5");
    	digest.update("109code=value2name=value3personid=value1202cb962234w4ers2aa".getBytes());
    	byte[] bs=digest.digest();
    	int i; 
    	StringBuffer buf = new StringBuffer(""); 
    	for (int offset = 0; offset < bs.length; offset++) { 
	    	i = bs[offset]; 
	    	if(i<0){
	    		i+= 256;
	    	}
	    	if(i<16){ 
		    	buf.append("0");
	    	}
		    buf.append(Integer.toHexString(i));
    	} 
    	System.out.println("result: " + buf.toString().toUpperCase());//32位的加密

    	System.out.println("result: " + buf.toString());//16位的加密

	}

	public static String encrypt32(String encryptStr) {
		MessageDigest md5;
		try {
			md5 = MessageDigest.getInstance("MD5");
			byte[] md5Bytes = md5.digest(encryptStr.getBytes());
			StringBuffer hexValue = new StringBuffer();
			for (int i = 0; i < md5Bytes.length; i++) {
				int val = ((int) md5Bytes[i]) & 0xff;
				if (val < 16)
					hexValue.append("0");
				hexValue.append(Integer.toHexString(val));
			}
			encryptStr = hexValue.toString();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return encryptStr;
	}

	public static String encrypt16(String encryptStr) {
		return encrypt32(encryptStr).substring(8, 24);
	}

	public static String MD5(String s) {
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9','a', 'b', 'c', 'd', 'e', 'f' };
		try {
			byte[] strTemp = s.getBytes();
			MessageDigest mdTemp = MessageDigest.getInstance("MD5");
			mdTemp.update(strTemp);
			byte[] md = mdTemp.digest();
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			return null;
		}
	}

	public static String getMD5(String content) {
		String s = makeMD5(content);
		String s1 = null;
		if (s != null) {
			s1 = s.substring(0, 16);
		}
		String s2 = null;
		if (s != null) {
			s2 = s.substring(16, 32);
		}
		s1 = makeMD5(s1);
		s2 = makeMD5(s2);
		s = s1 + s2;
		for (int i = 0; i < 100; i++) {
			s = makeMD5(s);
		}
		return s;
	}

	private static String makeMD5(String content) {
		try {
			MessageDigest messageDigest = MessageDigest.getInstance("MD5");
			messageDigest.update(content.getBytes());
			return getHashString(messageDigest);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return null;
	}

	private static String getHashString(MessageDigest messageDigest) {
		StringBuilder builder = new StringBuilder();
		for (byte b : messageDigest.digest()) {
			builder.append(Integer.toHexString(  ( b >> 4 ) & 0xf )  );
			builder.append(Integer.toHexString(  b & 0xf )           );
		}
		return builder.toString();
	}

}
